var Promise = require('bluebird');
var ObjectID = require('mongodb').ObjectID;
var cheerio = require('cheerio');

var mongoBase = require('../common/mongo-base');
var page = require('../common/page');


module.exports = {
  planeList: planeList,
  showPlane: showPlane,
  savePlane: savePlane,
  updatePlane: updatePlane,
  removePlane: removePlane
};


function planeList(req, res, next) {

  var pageNo = Math.floor(req.query.pageNo);
  var pageSize = Math.floor(req.query.pageSize);
  pageNo = pageNo ? pageNo: 1;
  pageSize = pageSize ? pageSize: 20;

  var query = {};

  var fields = {_id:1, manufacturer:1, firstTime:1, planeName:1, parameters:1};
  var options = {sort:{date:-1}, skip: (pageNo - 1) * pageSize, limit:pageSize};

  Promise.all([
    mongoBase.findData('plane-data', query, fields, options),
    mongoBase.countData('plane-data')
  ])

  .then(function(results) {
    var resData = {
      searchUrl: '/admin/planeSearch',
      pageUrl: '/admin/planeList',
      planeList: results[0],
      totalCount: results[1]
    }
    /*分页数据*/
    resData.page = page.getPage(pageNo, pageSize, resData.totalCount, 7);

    res.render('admin/plane-list', resData);
  })

  .catch(function(err){
    console.log(err);
  });

}


function showPlane(req, res, next) {
  var plaId = req.query.plaId;
  var parentId = req.query.parentId;
  var artId = req.query.artId;

  if(parentId) {
    plaId = parentId
  }

  if(!plaId) {
    res.render('admin/plane-edit', {title: '新增机型', detail: {detailKey: artId}});
    return;
  }

  var query = {_id: new ObjectID(plaId)};
  var fields = {_id:1, manufacturer:1, firstTime:1, planeName:1, photos:1, parameters:1, ellipsis:1, others:1, detailKey:1};

  mongoBase.findOneData('plane-data', query, fields)

    .then(function(data) {
      if(artId) {
        data.detailKey = artId;
      }
      var resData = {
        title: '编辑机型资料',
        detail: data
      }
      res.render('admin/plane-edit', resData);
    })

    .catch(function(err){
      console.log(err);
    });
}


function savePlane(req, res, next) {

  var postdata = JSON.parse(req.body.postdata);

  var planeData = {
    planeName: postdata.planeName,
    manufacturer: postdata.manufacturer,
    firstTime: postdata.firstTime,
    parameters: postdata.parameters,
    photos: postdata.photos,
    ellipsis: postdata.ellipsis,
    others: postdata.others,
    detailKey: postdata.detailKey,
    date: new Date()
  };

  mongoBase.saveData('plane-data', planeData)
    
    .then(function(data) {

      planeList(req, res, next);

    })

    .catch(function(err) {
      console.log(err);
    });

}


function updatePlane(req, res, next) {
  var postdata = JSON.parse(req.body.postdata);

  var planeData = {
    planeName: postdata.planeName,
    manufacturer: postdata.manufacturer,
    firstTime: postdata.firstTime,
    parameters: postdata.parameters,
    photos: postdata.photos,
    ellipsis: postdata.ellipsis,
    others: postdata.others,
    detailKey: postdata.detailKey,
    date: new Date()
  };

  var query = {_id: new ObjectID(postdata.plaId)};
  mongoBase.updateData('plane-data', query, planeData)
    
    .then(function(data) {

      planeList(req, res, next);

    })

    .catch(function(err) {
      console.log(err);
    });

}


function removePlane(req, res, next) {
  var plaId = req.query.plaId;

  var query = {_id: new ObjectID(plaId)};

  mongoBase.removeData('plane-data', query)

    .then(function() {

      planeList(req, res, next);

    })

    .catch(function(err){
      console.log(err);
    });
}

